package cn.edu.zxj.springblog.model;

import cn.edu.zxj.springblog.common.Constants;
import lombok.Data;

/**
 * Created with IntelliJ IDEA.
 * Description：统一返回结果的实体类:
 *
 * @author: zxj
 * @date: 2024-02-04
 * @time: 17:51:46
 */
@Data
public class Result<T> {
    // 业务状态码
    private Integer code;
    // 错误描述
    private String errorMessage;
    // 返回的数据
    private T data;

    /**
     * @description: 业务处理流程成功
     **/
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(Constants.RESULT_CODE_SUCCESS);
        result.setData(data);
        result.setErrorMessage("");
        return result;
    }


    /**
     * @description: 业务处理流程失败
     **/
    public static <T> Result<T> fail(String errorMessage) {
        Result<T> result = new Result<>();
        result.setCode(Constants.RESULT_CODE_SUCCESS);
        result.setErrorMessage(errorMessage);
        return result;
    }


    /**
     * @description: 业务处理流程失败, 失败时带回一些数据
     **/
    public static <T> Result<T> fail(String errorMessage,T data) {
        Result<T> result = new Result<>();
        result.setCode(Constants.RESULT_CODE_SUCCESS);
        result.setData(data);
        result.setErrorMessage(errorMessage);
        return result;
    }


    /**
     * @description: 用户未登录
     **/
    public static <T> Result<T> un_Login() {
        Result<T> result = new Result<>();
        result.setCode(Constants.RESULT_CODE_UN_LOGIN);
        result.setErrorMessage("用户未登录");
        return result;
    }
}
